package pl.put.augrelshooter.logic.game;

import java.util.ArrayList;
import java.util.List;

import pl.put.augrelshooter.logic.detection.DetectableTypeEnum;
import pl.put.augrelshooter.logic.detection.IDetectable;
import pl.put.augrelshooter.logic.game.badges.Badge;
import pl.put.augrelshooter.logic.game.weapon.Bazooka;
import pl.put.augrelshooter.logic.game.weapon.Machinegun;
import pl.put.augrelshooter.logic.game.weapon.Pistol;
import pl.put.augrelshooter.logic.game.weapon.Weapon;

public class Player implements IDetectable {
	private static final int MAX_HEALTH = 100;

	private String nick;
	private Badge badge;

	private int healthPoints;
	private Weapon currentWeapon;
	private List<Weapon> weapons;
	private int currentWeaponIndex;

	public Player(String nick, Badge badge) {
		this.nick = nick;
		this.badge = badge;
		this.healthPoints = MAX_HEALTH;
		weapons = new ArrayList<Weapon>();
		weapons.add(new Pistol());
		weapons.add(new Machinegun());
		weapons.add(new Bazooka());
		currentWeaponIndex = 0;

		equipWeapon(weapons.get(currentWeaponIndex));
		currentWeapon.addAmmo();
	}

	@Override
	public Badge getBadge() {
		return badge;
	}

	@Override
	public String getLabel() {
		return nick;
	}

	public Weapon getWeapon() {
		return currentWeapon;
	}

	/**
	 * Equips player with given weapon.
	 * 
	 * @param weapon
	 *            new weapon.
	 */
	public void equipWeapon(Weapon weapon) {
		this.currentWeapon = weapon;
	}

	public int getHealthPoints() {
		return healthPoints;
	}

	public void heal(int numberOfHealtpoints) {
		this.healthPoints = Math.min(MAX_HEALTH, healthPoints + numberOfHealtpoints);
	}

	/**
	 * Return health points after dealing damage.
	 * 
	 * @param damage
	 *            ammount of damage to deal.
	 * @return remaining health points.
	 */
	public int dealDamage(int damage) {
		this.healthPoints -= damage;

		return this.healthPoints;
	}

	@Override
	public DetectableTypeEnum getType() {
		return DetectableTypeEnum.PLAYER;
	}

	public void changeWeaponNext() {
		currentWeaponIndex = (currentWeaponIndex + 1) % weapons.size();
		equipWeapon(weapons.get(currentWeaponIndex));
	}

	public void changeWeaponPrev() {
		if (currentWeaponIndex == 0)
			currentWeaponIndex = weapons.size() - 1;
		else
			currentWeaponIndex -= 1;
		equipWeapon(weapons.get(currentWeaponIndex));
	}
}
